﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinDemo.Core;
 
namespace WinDemo
{
    public partial class FrmMain : Form, ILoadForm
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            PluginLoader.LoadAllPlugins();
            InitPlugin();
        }
        private void InitModule(Iplugin plugin)
        {
            PictureBox picture = new PictureBox();
            picture.BackColor = System.Drawing.Color.Transparent;
            picture.Image = plugin.ModulePicture;
            picture.InitialImage = null;
            picture.Dock = DockStyle.Left;
           // picture.Location = new System.Drawing.Point(42, 12);
            picture.Size = new System.Drawing.Size(65, 71);
            picture.TabStop = false;
            panelTop.Controls.Add(picture);
            picture.MouseEnter += (sender, e) =>
            {
                picture.Image = plugin.ModulePictureEnter;
            };
            picture.MouseLeave += (sender, e) =>
            {
                picture.Image = plugin.ModulePicture;
            };
            picture.MouseDown += (sender, e) =>
            {
                picture.Image = plugin.ModulePictureClick;
            };
            picture.MouseUp += (sender, e) =>
            {
                picture.Image = plugin.ModulePictureEnter;
            };
            picture.Click += (sender, e) =>
            {
                InitChildNodes(plugin);
            };
        }

        private void InitChildNodes(Iplugin plugin)
        {
             
            panelMenu.Controls.Clear();
            panelLeft.Controls.Clear();
            panelRight.Controls.Clear();
            foreach (KeyValuePair<string, EventHandler> item in plugin.ChildNodes)
            {
                Button btn = new Button();
                btn.Text = item.Key;
                btn.Click += item.Value;
                btn.Dock = DockStyle.Left;
                btn.BackColor = System.Drawing.Color.White;
                //btn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                btn.Font = new System.Drawing.Font("宋体", 9.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                btn.ForeColor = System.Drawing.Color.SteelBlue;
                btn.UseVisualStyleBackColor = false;
                panelMenu.Controls.Add(btn);
            }
            
        }

        public void InitPlugin()
        {
            foreach (Iplugin plugin in PluginLoader.plugins)
            {
                plugin.FormLoader = this;
                InitModule(plugin);
                
            }
        }



        public void LoadFrm(Form frm)
        {
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.ShowIcon = false;
            frm.ShowInTaskbar = false;
            frm.BackColor = Color.White;
            frm.TopLevel = false;
            frm.Dock = DockStyle.Fill;
            panelLeft.Controls.Add(frm);
            frm.Show();
            frm.BringToFront();
            frm.Focus();
        }

      

        public void LoadNavFrm(Form frm)
        {
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.ShowIcon = false;
            frm.ShowInTaskbar = false;
            frm.BackColor = Color.White;
            frm.TopLevel = false;
            frm.Dock = DockStyle.Fill;
            panelLeft.Controls.Add(frm);
            frm.Show();
            frm.BringToFront();
            frm.Focus();
        }

        public void LoadContentFrm(Form frm)
        {
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.ShowIcon = false;
            frm.ShowInTaskbar = false;
            frm.BackColor = Color.Blue;
            frm.TopLevel = false;
            frm.Dock = DockStyle.Fill;
            panelRight.Controls.Add(frm);
            frm.Show();
            frm.BringToFront();
            frm.Focus();
        }
    }
}
